// pages/index/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: {},
    date2: {},
    month: '',
    month1:'',
    week: ['日', '一', '二', '三', '四', '五','六'],
    months:['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月']
  },
  // dayClick:function(e){
  //   console.log(e)
  // },
  /**
   * 生命周期函数--监听页面加载
   */
  dates: function() {
    var date = new Date();
    //当前是哪一年
    var year = date.getFullYear();
    // console.log(year)
    //当前是哪个月，注意这里的月是从0开始计数的
    var month = date.getMonth();
    var month1 = date.getMonth() + 1;
    // console.log(month1)
    
    //当前月的第一天的日期
    var firstDay = new Date(year, month, 1);
    var firstDay1 = new Date(year, month1, 1);
    // console.log(firstDay)
    //第一天是星期几
    var weekday = firstDay.getDay();
    var weekday1 = firstDay1.getDay();
    this.setData({
      weekday1: weekday1
    })
    // console.log(weekday1)
    var days = new Date(year, month + 1, 0).getDate();
    var days1 = new Date(year, month1 + 1, 0).getDate();
    // console.log(days)
    

    //今天是哪一天
    var todays = date.getDate()

    //今天是星期几
    var weektoday = new Date(year, month, todays);
    // console.log(weektoday)
    var res = []
    for (var i = 0; i < weekday; i++) {
      res.push({});
    }
    for (var j = 1; j < todays; j++) {
      res.push({ name: j,types:1 })
    }
    for (var j = todays; j <= days; j++) {
      res.push({ name: j})
    }
    res.forEach((val,index)=>{
      if (val.name == todays){
        this.setData({
          dateIndex:index,
          todays: todays
        })
        // console.log(index)
      }
    })
    //2
    var res2 = []
    for (var i = 0; i < weekday1; i++) {
      res2.push({});
    }
    for (var j = 1; j < todays; j++) {
      res2.push({ name: j })
    }
    for (var j = todays; j <= days1; j++) {
      res2.push({ name: j, types: 1})
    }

    // console.log(todays)
    // console.log(days1)
    
    let m1 = this.data.months[month]
    let m2 = this.data.months[month1]

    this.setData({
      weekday: weekday,
      monthNum: month,
      monthNum1: month1,
      month:m1,
      month1: m2,
      date: res,
      date2: res2,
    })
    
  },
  daysClick:function(e){
    // let item = e.currentTarget.dataset.item
    let that = this

    let weekday = this.data.weekday
    let index = e.currentTarget.dataset.index
    
    let res = this.data.date
    if (!res[index].types) {
      this.setData({
        dateIndex: index,
        dateIndex2: '50'
      })

      wx.setStorageSync('month', that.data.monthNum +1)
      wx.setStorageSync('Day', index - weekday+1)
      
      wx.switchTab({
        url: `/pages/index/homePage/index`
      })

    }

  },
  daysClick2: function (e) {
    let that = this
    let index = e.currentTarget.dataset.index
    // console.log(index -that.data.weekday1)
    let res = this.data.date2

    if (!res[index].types) {
      this.setData({
        dateIndex: '50',
        dateIndex2: index
      })
      wx.setStorageSync('month', that.data.monthNum1 + 1)
      wx.setStorageSync('Day', index - that.data.weekday1 + 1)

      wx.switchTab({
        url: `/pages/index/homePage/index`
      })
    }
    
  },
  onLoad: function(options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.dates()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})